package com.tamir.template.bean;

import com.tamir.template.context.TemplateContext;
import com.tamir.template.util.LogicOperateUtil;
import com.tamir.template.util.OperaterUtil;

import java.math.BigDecimal;

/**
 * @author : tanggh
 * @description : 逻辑运算操作
 * @date : 2016/11/8
 */
public class LogicOperate
{
    //操作符
    private String operator;

    //左表达
    private LogicOperate leftOperate;

    //右表达式
    private LogicOperate rightOperate;

    //值表达式
    private String express;

    //值表达式
    private Object expressObject;

    //入参表达式关键字
    private String inputKey;

    //取非
    private boolean isNon;

    //逻辑表达式的父结点
    private TNode parentNode;


    public String getInputKey()
    {
        return inputKey;
    }

    public void setInputKey(String inputKey)
    {
        this.inputKey = inputKey;
    }

    public TNode getParentNode()
    {
        return parentNode;
    }

    public void setParentNode(TNode parentNode)
    {
        this.parentNode = parentNode;
    }

    public String getOperator()
    {
        return operator;
    }

    public void setOperator(String operator)
    {
        this.operator = operator;
    }

    public LogicOperate getLeftOperate()
    {
        return leftOperate;
    }

    public void setLeftOperate(LogicOperate leftOperate)
    {
        this.leftOperate = leftOperate;
    }

    public LogicOperate getRightOperate()
    {
        return rightOperate;
    }

    public void setRightOperate(LogicOperate rightOperate)
    {
        this.rightOperate = rightOperate;
    }

    public String getExpress()
    {
        return express;
    }

    public void setExpress(String express)
    {
        this.express = express;
    }

    public Object getExpressObject()
    {
        return expressObject;
    }

    public void setExpressObject(Object expressObject)
    {
        this.expressObject = expressObject;
    }

    public boolean isNon()
    {
        return isNon;
    }

    public void setNon(boolean non)
    {
        isNon = non;
    }

    public static void main(String[] args)
    {
        String str = "(a ||c && (c=d))".replaceAll("\\s*", "");
        str = "5 + ((1 + 2) * 4) - 3".replaceAll("\\s*", "");;
        LogicOperate rootLogicOperator = LogicOperateUtil.parseExpress(str);
        System.out.println(LogicOperate.getValue(rootLogicOperator, null));

    }

    /**
     * 获取值
     * @param operate
     * @param context
     * @return
     */
    public static Object getValue(LogicOperate operate, TemplateContext context) {
        Object retVal = null;
        if(operate != null){
            Object leftObj = null;
            Object rightObj = null;
            if(operate.getOperator() != null){
                leftObj = getValue(operate.getLeftOperate(), context);
                rightObj = getValue(operate.getRightOperate(), context);
                retVal = OperaterUtil.calObject(operate.getOperator(), leftObj, rightObj, null,
                        operate);
                if(operate.isNon()){
                    if(retVal instanceof Boolean){
                        retVal = !(Boolean)retVal;
                    }else{
                        retVal = !(new Boolean(retVal.toString()));
                    }
                }
            }else{
                retVal = new BigDecimal(operate.getExpress());
            }
        }
        return retVal;
    }
}
